package com.tyk.bookstore.front.order.controller;


import com.tyk.bookstore.front.common.core.model.result.R;
import com.tyk.bookstore.front.common.core.model.vo.PageVo;
import com.tyk.bookstore.front.order.model.query.OrderAddressUpdateQuery;
import com.tyk.bookstore.front.order.model.query.OrderCancelQuery;
import com.tyk.bookstore.front.order.model.query.OrderPageQuery;
import com.tyk.bookstore.front.order.model.query.OrderSubmitQuery;
import com.tyk.bookstore.front.order.model.vo.*;
import com.tyk.bookstore.front.order.service.OrderService;
import com.tyk.bookstore.front.order.vo.OrderAddressVo;
import com.tyk.bookstore.front.order.vo.OrderSplitVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author tyk
 * @description 订单管理
 */
@Tag(name = "订单管理", description = "订单管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("api/order")
public class OrderController {

    private final OrderService orderService;

    @Operation(summary = "生成订单", description = "生成订单")
    @GetMapping({"create"})
    public R<OrderCreateVo> createOrder() {
        return R.ok(orderService.createOrder());
    }


    @Operation(summary = "提交订单", description = "提交订单")
    @PostMapping({"submit"})
    public R<OrderSubmitVo> submitOrder(
            @Validated @RequestBody OrderSubmitQuery query
    ) {
        OrderSubmitVo orderSubmitVo = orderService.submitOrder(query);
        return R.ok(orderSubmitVo);
    }

    @PostMapping({"go-pay/{orderId}"})
    @Operation(summary = "获取待支付订单支付信息", description = "获取待支付订单支付信息")
    public R<OrderSubmitVo> getWaitPayInfo(
            @Schema(description = "订单ID") @NotNull @Min(value = 1)
            @PathVariable Long orderId
    ) {
        OrderSubmitVo orderSubmitVo = orderService.getWaitPayInfo(orderId);
        return R.ok(orderSubmitVo);
    }

    @PutMapping({"cancel"})
    @Operation(summary = "取消订单", description = "取消订单")
    public R<Object> cancel(
            @Validated @RequestBody OrderCancelQuery query
    ) {
        orderService.cancel(query);
        return R.ok();
    }

    @GetMapping({"{id}"})
    @Operation(summary = "获取订单详情", description = "获取订单详情")
    public R<OrderVo> getById(
            @Schema(description = "订单ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(orderService.getById(id));
    }

    @GetMapping({"split/{id}"})
    @Operation(summary = "获取拆单详情", description = "获取拆单详情")
    public R<OrderSplitVo> getSplitById(
            @Schema(description = "订单ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(orderService.getSplitById(id));
    }

    @GetMapping({"page"})
    @Operation(summary = "分页查询订单", description = "分页查询订单")
    public R<OrderPageVo> getPage(
            @Validated @ParameterObject OrderPageQuery query,
            BindingResult br
    ) {
        return br.hasErrors() ? R.ok(PageVo.instance(OrderPageVo.class)) : R.ok(orderService.getPage(query));
    }

    @PutMapping({"confirm/{id}"})
    @Operation(summary = "确认收货", description = "确认收货")
    public R<Object> confirmById(
            @Schema(description = "订单ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        orderService.confirmById(id);
        return R.ok();
    }

    @DeleteMapping({"{id}"})
    @Operation(summary = "删除订单", description = "删除订单")
    public R<Object> deleteById(
            @Schema(description = "订单ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        orderService.deleteById(id);
        return R.ok();
    }

    @DeleteMapping
    @Deprecated
    @Operation(summary = "批量删除订单", description = "批量删除订单")
    public R<Object> deleteBatchByIdSet(
            @NotNull @NotEmpty Set<Long> idList
    ) {
        orderService.deleteBatchByIdSet(idList);
        return R.ok();
    }

    @Operation(summary = "获取订单收货地址", description = "获取订单收货地址")
    @GetMapping("address/{id}")
    public R<OrderAddressVo> getAddressById(
            @Schema(description = "订单ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(orderService.getAddressById(id));
    }

    @Operation(summary = "更新订单收货地址", description = "更新订单收货地址")
    @PutMapping("address")
    public R<Object> updateAddress(
            @Validated @RequestBody OrderAddressUpdateQuery query
    ) {
        orderService.updateAddress(query);
        return R.ok();
    }

    @Operation(summary = "待评价商品列表", description = "待评价商品列表")
    @GetMapping("comment/wait/{id}")
    public R<List<WaitCommentVo>> getWaitCommentList(
            @Schema(description = "订单ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(orderService.getWaitCommentList(id));
    }


}
